package com.uw.uwrpc.core.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import okhttp3.ConnectionPool;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

import java.util.concurrent.TimeUnit;

@Slf4j
public class HttpUtil {

    private static OkHttpInvoker Default = new OkHttpInvoker(500);

    @SneakyThrows
    public static <T> T httpGet(String url, Class<T> clazz) {
        log.debug(" =====>>>>>> httpGet: " + url);
        String respJson = Default.get(url);
        log.debug(" =====>>>>>> response: " + respJson);
        return JSON.parseObject(respJson, clazz);
    }

    @SneakyThrows
    public static <T> T httpGet(String url, TypeReference<T> typeReference) {
        log.debug(" =====>>>>>> httpGet: " + url);
        String respJson = Default.get(url);
        log.debug(" =====>>>>>> response: " + respJson);
        return JSON.parseObject(respJson, typeReference);
    }

    @SneakyThrows
    public static <T> T httpPost(String requestString, String url, Class<T> clazz) {
        log.debug(" =====>>>>>> httpGet: " + url);
        String respJson = Default.post(requestString, url);
        log.debug(" =====>>>>>> response: " + respJson);
        return JSON.parseObject(respJson, clazz);
    }


    public static class OkHttpInvoker {

        final static MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8");

        OkHttpClient client;

        public OkHttpInvoker(int timeout) {
            client = new OkHttpClient.Builder()
                    .connectionPool(new ConnectionPool(16, 60, TimeUnit.SECONDS))
                    .readTimeout(timeout, TimeUnit.MILLISECONDS)
                    .writeTimeout(timeout, TimeUnit.MILLISECONDS)
                    .connectTimeout(timeout, TimeUnit.MILLISECONDS)
                    .retryOnConnectionFailure(true)
                    .build();
        }

        public String get(String url) {
            log.debug(" ===> get url = " + url);
            Request request = new Request.Builder()
                    .url(url)
                    .get()
                    .build();
            try {
                String respJson = client.newCall(request).execute().body().string();
                log.debug(" ===> respJson = " + respJson);
                return respJson;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        public String post(String requestString, String url) {
            log.debug(" ===> post  url = {}, requestString = {}", requestString, url);
            Request request = new Request.Builder()
                    .url(url)
                    .post(RequestBody.create(requestString, JSONTYPE))
                    .build();
            try {
                String respJson = client.newCall(request).execute().body().string();
                log.debug(" ===> respJson = " + respJson);
                return respJson;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

}
